package com.demo.caizi.aop;

import com.demo.caizi.biz.domain.CustomInfo;
import com.demo.caizi.biz.domain.TradeInfo;
import com.demo.caizi.telemetry.MetricsLogPOJO;
import com.demo.caizi.telemetry.TelemetryLog;
import com.demo.caizi.telemetry.TradeInfoMetrics;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import uk.co.jemos.podam.api.PodamFactory;

import java.util.Date;

@Aspect
@Component
public class TradingMetricsActionAop {

//    @Pointcut("@annotation(MetricsAction)")
//    public void pointcut(MetricsAction metricsAction){
//
//    }
    @Autowired
    PodamFactory podamFactory;


    @Around("@annotation(tradingMetricsAction)")
    public Object around(ProceedingJoinPoint joinPoint, TradingMetricsAction tradingMetricsAction) throws Throwable {
        TradeInfoMetrics tradeInfoMetrics = new TradeInfoMetrics();
//        CustomInfo customInfo = CustomContextHolder.getHolder().getCustomInfo();

        CustomInfo customInfo = podamFactory.manufacturePojo(CustomInfo.class);
        BeanUtils.copyProperties(customInfo,tradeInfoMetrics);
        tradeInfoMetrics.setMetricsName(tradingMetricsAction.metricsName());
        tradeInfoMetrics.setFeatureName(tradingMetricsAction.featureName());
        tradeInfoMetrics.setVerb(tradingMetricsAction.verb());
        tradeInfoMetrics.setSubVerb(tradingMetricsAction.subVerb());
        tradeInfoMetrics.setDate(new Date());

        Object[] args=joinPoint.getArgs();
        if (null != args && args.length>0
                && args[0] instanceof TradeInfo tradeInfo){
            TradeInfoMetrics.TradeInfo trade = new TradeInfoMetrics.TradeInfo();
            BeanUtils.copyProperties(tradeInfo,trade);
            tradeInfoMetrics.setTradeInfo(trade);
        }
        TelemetryLog.log(tradeInfoMetrics);

        return joinPoint.proceed();
    }
}
